from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
class UserManager(BaseUserManager):
    # _表示是受保护的，只能在这个类中可以调用
    def _create_user(self, username, password, email, **kwargs):
        if not username:
            raise ValueError("The given username must be set")
        if not password:
            raise ValueError('Please enter your password !')
        if not email:
            raise ValueError('Please enter your email !')

        email = self.normalize_email(email)
        user = self.model(username=username, password=password, email=email, **kwargs)
        user.set_password(password)
        user.save(using=self._db)
        return user

    # 创建普通用户
    def create_user(self, username, password, email, **kwargs):
        return self._create_user(username=username, password=password, email=email, **kwargs)


class User(AbstractUser):
    # 不要的字段重写赋值为None即可
    first_name = None
    last_name = None
    user_permissions = None
    is_superuser = None
    is_staff = None
    signature = models.CharField(max_length=255, default='这个人很懒，还没有设置个性签名')
    email = models.EmailField(unique=True, max_length=254, verbose_name='email address')
    profile = models.CharField(max_length=255)
    objects = UserManager()
